/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include <jni.h>
#include <string>
#include <atomic>

#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "extensions/ExtensionExport.h"

#include "ActCmd.h"
#include "RemoteControlHelper.h"
#include "BlueToothHelper.h"

#include "base/CCScheduler.h"
#include "extensions/cocos-ext.h"
#include "network/SocketIO.h"
#include "SocketIOHelper.h"
#include "network/HttpClient.h"
#include "SIOUserInfo.h"

USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::network;
using namespace std;

class HelloWorld : public cocos2d::Scene,public cocos2d::extension::TableViewDataSource, public cocos2d::extension::TableViewDelegate,public BlueToothHelperDelegate,public SocketIOHelperDelegate
{
public:
    static unsigned int BLE_STATUE_STOP;
    static unsigned int BLE_STATUE_SCANING;
    static unsigned int BLE_STATUE_CONNECTING;
    static unsigned int BLE_STATUE_CONNECTED;
    static cocos2d::Scene* createScene();
    static cocos2d::extension::TableView* bleTabView;
    static std::atomic<unsigned int> currBleStatus;

    //static HelloWorld* me;
    void initPromptLayer();

    virtual bool init() override;
    virtual void onEnterTransitionDidFinish() override;
    //virtual void onExitTransitionDidStart() override;

    void initDrawTest() ;

    void setNotifyLabel(std::string) override;
    void setKeyAct(ControlCode &) override ;

    //-SIO---------------------
    void SioConnection(SIOClient *, const std::string&) override;
    void SioOnline(SIOClient *, const std::string&) override;
    void SioStatInfo(SIOClient *, const std::string&) override;
    void SioStoreRoomid(SIOClient *, const std::string&) override;
    void SioShareCode(SIOClient *, const std::string&) override;
    void SioOffline(SIOClient *, const std::string&) override;
    void SioTalking(SIOClient *,const std::string &) override;
    void SioRoomCommentator(SIOClient *, const std::string& ) override;
    void SioOnConnect(SIOClient* ) override;
    void SioOnMessage(SIOClient* , const std::string& ) override;
    void SioOnError(cocos2d::network::SIOClient* , const std::string& ) override;
    void SioOnClose(cocos2d::network::SIOClient* ) override;
    void SioRoomVisitors(SIOClient *, const std::string& ) override;
    void SioJoinRoom();
    void initSocketIO(bool isForceConn=false);

    std::atomic<bool> sioClientIsConnected;

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    //开始连接蓝牙
    void beginScanBLE();

    //放下一个命令
    virtual void setOneAct(ActCmd &) override;


    void initBlueList();
    void initKeyBoard();
    void initBg();


    //当前BLE的 CellNum    TableViewCell *cellAtIndex(ssize_t idx);
    static int curBleCellIdx;
    static void chooseBleList(int curBleCellIdx);
    static void resetOneBle(int resetBleCellIdx);
    static void connectBleList(int curBleCellIdx);

    static void BLE_StatusChange(unsigned int changeCode,int deviceType);

    static bool isDisplayDetail;//是否显示在具体窗

    /*表格的表白*/
    virtual void scrollViewDidScroll(cocos2d::extension::ScrollView* view)override {};
    virtual void scrollViewDidZoom(cocos2d::extension::ScrollView* view)override {}
    virtual void tableCellTouched(cocos2d::extension::TableView* table, cocos2d::extension::TableViewCell* cell)override;
    virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx)override;
    virtual cocos2d::extension::TableViewCell* tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx)override;
    virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table)override;

    void downloadFriend(std::string url,std::string id);
    void onHttpRequestCompleted(HttpClient *sender, HttpResponse *response);
    void downFailed(const std::string id,const std::string url);
    void downSuccess(const std::string &id,const  std::string &url, std::vector<char> &cnt);

    static Scene* createClassPathChooseScene();

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

private:
    std::string saveConnectMadId;
    void initGlobal();
    static void checkoutGlobal();
    void initStoreUUIDAndRegister();
    void beforeDestoryRunning();
    void destoryRunning();
    cocos2d::Label*  displayLabel;
    cocos2d::Label*  connectBleLabel;
    float blueToothY;
    Sprite *runningSprite;
    Node *oneUserNode;
    ValueMap loginInInfo;
    void writeGameUUIDInfo();
    void onlineTest();
    void storeUserInfoToLocal(bool isLoadingUseFace=true);
    void initUserInfoFromLocal();
    SIOUserInfo loginUserInfo;
    void initLoginUserIcon();
    Rect runningBoundingBoxSize;
    void searchingBle(float dt);
    bool isNeedSaveUserInfo;
};

#endif // __HELLOWORLD_SCENE_H__
